<?php


include_once('init.php');
// print_r($_POST);
if (!empty($_POST)) {
    //实现数据库链接mysqli_connect的函数，数据不为空时要执行的操作
    //从$_POST获取到的用户数据存到临时变量中
    $username = $_POST['username'];
    $password=md5($_POST['password']);

    // if (!empty($_COOKIE['username']) && $_COOKIE['password'] == md5($_POST['password'])) {
    //     $password = $_COOKIE['password'];
    // } else {

    //     if (!empty($_COOKIE['username']) && $_COOKIE['password'] == $_POST['password']) {
    //         $password = $_COOKIE['password'];
    //     }  
    // }

    if (!empty($_COOKIE['username']) && $_COOKIE['password'] == $_POST['password']) {
            $password = $_COOKIE['password'];
    } 

    //链接数据库数据
   
    //从$_POST获取到的用户数据存到临时变量中
    // $username=$_POST['username'];
    // // $password=md5($_POST['password']);
    // if(!empty($_COOKIE['username']) && $_COOKIE['password'] == md5($_POST['password'])){
    //       $password=$_COOKIE['password'];
    // }else if(!empty($_COOKIE['username']) && $_COOKIE['password'] == ($_POST['password'])){
    // 	$password=$_COOKIE['password'];
    // }else{
    // 	echo '<script>alert("用户名或密码错误");history.go(-1);</script>';die;
    // }
    //查询用户数据的语句

    // getSelectByOne('admin','$_POST',);
    $sql = "select * from `admin` where `username`='{$username}' and `password`='{$password}'";
    // echo $sql;exit;
    //执行sql的语句，把结果存到一个临时变量里面
    $result = mysqli_query($link, $sql);
    //从查询的结果中提取真实的数据并存到一个临时变量里面
    $data = mysqli_fetch_assoc($result);
    //如果数据不为空时
    if (!empty($data)) {
        session_start();
        $_SESSION['admin'] = $data;
        if (!empty($_POST['check'])) {
            // echo 1;exit;
            setcookie('username', $username, time() + 3600 * 24);
            setcookie('password', $password, time() + 3600 * 24);
        } else {
            setcookie('username', '', 0);
            setcookie('password', '', 0);
        }

        if(!empty($_POST['auto'])){

            setcookie('auto_login',1, time() + 3600 * 24);

        }else{

            setcookie('auto_login', '0', 0);
        }



        // echo '<script>alert("登陆成功");</script>';die;
        header('location:index.php');
        //显示登陆成功提示
    } else {
        echo '<script>alert("用户名或密码错误");history.go(-1);</script>';
        die;
        //否则就显示错误
    }
}


include (VIEW_PATH.$admin.'layout.html');

?>

